package myjava.algorithm.leetcode;

public class Candy {
    public static int candy(int[] ratings) {
    	
    	if(ratings == null || ratings.length ==0 )
    		return 0;
    	
        int result = 0;
        
        int[] array = new int[ratings.length];
        for(int i = 0;i<array.length;i++)
        {
        	array[i] = 1;
        }
        
        for(int i = 1;i<array.length;i++)
        {
        	if(ratings[i]>ratings[i-1])
        	{
        		array[i] = array[i-1]+1; 
        	}
        }
        
        for(int i = array.length -2;i>=0;i--)
        {
        	if(ratings[i] > ratings[i+1])
        	{
        		array[i] = Math.max(array[i],array[i+1]+1);
        	}
        }
        
        for(int i = array.length -1;i>=0;i--)
        {
        	result += array[i];
        }
        
        return result;
    }
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		int[] ratings = {1,3,5};
		System.out.println(candy(ratings));
	}

}
